package com.dalin.system.service.imple;

import com.dalin.system.dao.SystemUserMapper;
import com.dalin.system.pojo.entity.SystemUserEntity;
import com.dalin.framework.BaseServiceImple;
import com.dalin.framework.vos.PageInfoVo;
import com.dalin.framework.vos.ResResult;
import com.dalin.system.pojo.request.SystemUserRequest;
import com.dalin.system.pojo.response.SystemUserVo;
import com.dalin.system.service.SystemUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SystemUserServiceImpl extends BaseServiceImple implements SystemUserService {
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Override
    public ResResult listPageSystemUsers(SystemUserRequest req) {
        log.info("listPageSystemUsers params:"+req.getPageNum() + "," + req.getPageSize());
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List l = systemUserMapper.listSystemUsers(req);
        PageInfoVo piv = new PageInfoVo(l);
        piv.setList(getSystemUserVos(l));
        return ResResult.getSuccessRes(piv);
    }
    public List<SystemUserVo> getSystemUserVos(List<SystemUserEntity> orderInfoList) {
        return orderInfoList.stream().map(entity -> {
            SystemUserVo vo = new SystemUserVo();
            BeanUtils.copyProperties(entity, vo);
            vo.setUserId(entity.getUserId()+"");
            //vo.setDbTypeDisplay(entity.getDbTypeDisplay());
            return vo;
        }).collect(Collectors.toList());
    }

    public Long addOrUpdateSystemUser(SystemUserEntity systemUser) {
        if (systemUser.getUserId() == null || systemUser.getUserId() == 0) {
            
            systemUserMapper.insert(systemUser);
        } else {
            systemUserMapper.updateById(systemUser);
        }
        return systemUser.getUserId();
    }



    public Long deleteSystemUser(Long id) {
        int i = systemUserMapper.deleteById(id);
        System.out.println("systemUserMapper.deleteById("+id+")return ===="+i);
        return id;
    }

    public void deleteSystemUsers(List<String> ids) {
        int i = systemUserMapper.deleteBatchIds(ids);
        // return id;
    }

    @Override
    public SystemUserEntity getSystemUserById(int id) {
        return systemUserMapper.selectById(id);
    }

    @Override
    public SystemUserEntity findByUsername(String loginName,String loginPswd) {
        SystemUserEntity systemUser = new SystemUserEntity();
        systemUser.setLoginName(loginName);
        System.out.println("systemUserMapper:==="+systemUserMapper);
        SystemUserEntity user= systemUserMapper.findByUsername(systemUser);
        if (user != null) {
            return user;
        }
        return null;
    }
}